#ifndef IRCONTROLLER_H
#define IRCONTROLLER_H

#include <Arduino.h>
//#include "myhardware.h"
#include "ErrorCodes.h"
#include "Device.h"

// NEC protocol parameters
#define BUFFERSIZE 40
#define IR_NUM_BITS 32     // number of bits sent by IR remote
#define FirstLastBit 15      // divide 32 bits into two 15 bit chunks for integer variables. Ignore center two bits. they are all the same.
#define BIT_1_MSEC 1600           // Binary 1 threshold (Microseconds)
#define BIT_1_MSEC_MAX 2500
#define BIT_0_MSEC 450            // Binary 0 threshold (Microseconds)
#define START_PULSE_MSEC 4000 // Start bit threshold (Microseconds)
#define VERIFY_REMOTE 16128  // verifies first bits are 11111100000000. Different remotes may have different start codes

//! Receive remote codes from an IR remote
/*!
Receive remote codes from an IR remote. Interrupt driven\n
Supports NEC protocol remote controls (most are NEC protocol)\n
Requires the Device.h library and the PCInterrupt.h library\n
Be sure to set the pin to match where your IR receiver is connected.\n
The decode() routine takes the raw response and maps to a command.\n
You will need to modify IRMapping.h to match the codes generated by your remote\n
HARDWARE - Any Infrared digital receiver.
Currently only supports NEC protocol for transmitter\n
e.g. http://www.dfrobot.com/wiki/index.php?title=IR_Kit%28SKU:DFR0107%29
\version 1.3
*/
class IRController : public Device
{
 public:
  IRController();
  int16_t read();
  int16_t begin(int pin, DeviceType deviceType);
  uint16_t pulseMsec[IR_NUM_BITS+5];

 private:
   int interruptID;

   virtual int decode(int key);
   void pulse_to_bits(unsigned char bits[], int numBits);
   int verifyRemote(unsigned char bits[]);
   int bits_to_int(unsigned char bits[], int zeroBitCnt);
};

#endif // IRCONTROLLER_H
